package com.stormrage.edu.serv.edu.view.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.edu.view.EduMajorModuleDao;
import com.stormrage.edu.serv.edu.view.EduMajorModuleServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.edu.view.EduMajorModule;
import com.stormrage.entity.learn.LeaTerm;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class EduMajorModuleServImpl implements EduMajorModuleServ {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorModuleServImpl.class);

	@Value("#{constants['test.status']}")
	private String testStatus;

	@Autowired
	private EduMajorModuleDao eduMajorModuleDao;

	@Autowired
	private LeaTermServ leaTermServ;

	@Override
	public Integer create(final EduMajorModule arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 Create 】- 开始运行 ");

		if ("no".equals(testStatus)) {
			// ------------------ 判断学期是否创建重复 ------------------
			LeaTerm term = leaTermServ.queryByMajorIdDate(arg0.getMajorId());
			if (null != term)
				throw StormrageTechException.instance(ErrCons.SERV_ERR, "学期已开始，不能添加模组", StormrageTechException.ERR_APP,
						"【 EduMajorModuleServ 】-【 Create 】");
		}

		Integer index = eduMajorModuleDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void deleteById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteById 】- 开始运行 ");

		if ("no".equals(testStatus)) {
			// ------------------ 判断学期是否创建重复 ------------------
			EduMajorModule entity = queryById(arg0);
			LeaTerm term = leaTermServ.queryByMajorIdDate(entity.getMajorId());
			if (null != term)
				throw StormrageTechException.instance(ErrCons.SERV_ERR, "学期已开始，不能添加模组", StormrageTechException.ERR_APP,
						"【 EduMajorModuleServ 】-【 Create 】");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduMajorModuleDao.deleteById(map);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteById 】- 完成运行 ");
	}

	@Override
	public void deleteByMajorId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteByMajorId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduMajorModuleDao.deleteByMajorId(map);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteByMajorId 】- 完成运行 ");
	}

	@Override
	public void deleteByModuleId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteByModuleId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduMajorModuleDao.deleteByModuleId(map);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 DeleteByModuleId 】- 完成运行 ");
	}

	@Override
	public Integer queryCountById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 QueryCountById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		Integer count = eduMajorModuleDao.queryCountById(map);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 QueryCountById 】- 完成运行 ");
		return count;
	}

	@Override
	public EduMajorModule queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 queryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduMajorModule entity = eduMajorModuleDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduMajorModuleServ 】- 【 queryById 】- 完成运行 ");
		return entity;
	}

}
